using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Runtime.Serialization;
using System.Drawing.Drawing2D;
using OpenComposite.Workflow.Properties;
using System.Reflection;
using System.Drawing.Design;
using OpenComposite.Workflow.Interfaces;
using OpenComposite.Workflow.Extenders;

namespace OpenComposite.Workflow
{
	[Designer(typeof(DataAggregatorActivityDesigner))]
	[ToolboxItem(typeof(DataAggregatorActivityToolboxItem))]
	[ToolboxBitmap(typeof(resfinder), "OpenComposite.Workflow.Resources.mapping.png")]
	[ActivityValidator(typeof(DataAggregatorActivityValidator))]
	public partial class DataAggregatorActivity
		: Activity, ILogicalActivity, ITextActivity
	{
		#region Initialization
		public DataAggregatorActivity()
		{
			InitializeComponent();
		}
		#endregion

		#region Public Members

		#region Properties

		#region ActivitiesClass
		public static DependencyProperty ActivitiesClassProperty =
			DependencyProperty.Register("ActivitiesClass", typeof(object),
										typeof(DataAggregatorActivity), new PropertyMetadata(null));
		[Category("Data Aggregator")]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public object ActivitiesClass
		{
			get { return base.GetValue(DataAggregatorActivity.ActivitiesClassProperty); }
			set { base.SetValue(DataAggregatorActivity.ActivitiesClassProperty, value); }
		}
		#endregion

		#region LogicalActivityId
		public static DependencyProperty LogicalActivityIdProperty =
			DependencyProperty.Register("LogicalActivityId", typeof(int),
										typeof(DataAggregatorActivity), new PropertyMetadata(null));
		//[Browsable(false)]
		[Category("Data Aggregator")]
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public int LogicalActivityId
		{
			get { return (int)GetValue(LogicalActivityIdProperty); }
			set { SetValue(LogicalActivityIdProperty, value); }
		}
		#endregion

		#region LogicalActivityName
		public static DependencyProperty LogicalActivityNameProperty =
			DependencyProperty.Register("LogicalActivityName", typeof(string),
										typeof(DataAggregatorActivity), new PropertyMetadata(null));
		[Browsable(false)]
		[Category("Logical Activity")]
		public string LogicalActivityName
		{
			get { return (string)GetValue(DataAggregatorActivity.LogicalActivityNameProperty); }
			set { SetValue(DataAggregatorActivity.LogicalActivityNameProperty, value); }
		}
		#endregion

		#region Text
		public static DependencyProperty TextProperty = DependencyProperty.Register(
			"Text", typeof(string), typeof(DataAggregatorActivity), new PropertyMetadata(""));
		[Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
		[DefaultValue("")]
		[Browsable(true)]
		public string Text
		{
			get
			{
				string text = (string)base.GetValue(DataAggregatorActivity.TextProperty);
				if ( string.IsNullOrEmpty(text) ) return this.Name;
				return text;
			}
			set { base.SetValue(DataAggregatorActivity.TextProperty, value); }
		}

		DependencyProperty ITextActivity.GetTextProperty()
		{
			return TextProperty;
		}
		#endregion

		#endregion

		#endregion

		#region Protected Members

		#region Methods
		// override
		protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
		{
			base.Invoke(this.ContinueAt, EventArgs.Empty);
			return ActivityExecutionStatus.Executing;
		}
		private void ContinueAt(object sender, EventArgs e)
		{
			ActivityExecutionContext context = sender as ActivityExecutionContext;
			object activitiesclass = this.ActivitiesClass;
			Type t = activitiesclass.GetType();
			if ( t != null ) {
				MethodInfo mi = t.GetMethod(this.LogicalActivityName);
				if ( mi != null ) {
					mi.Invoke(activitiesclass, null);
				}
			}
			context.CloseActivity();
		}
		#endregion

		#endregion
	}

	#region Class: DataAggregatorActivityToolboxItem : Base: ActivityToolboxItem
	[Serializable]
	public class DataAggregatorActivityToolboxItem : ActivityToolboxItem
	{
		public DataAggregatorActivityToolboxItem(Type type)
			: base(type)
		{
			base.DisplayName = "Aggregator";
		}

		private DataAggregatorActivityToolboxItem(SerializationInfo info, StreamingContext context)
		{
			Deserialize(info, context);
		}
	}
	#endregion

	#region Class: DataAggregatorActivityDesigner : Base: ActivityDesigner
	[ActivityDesignerTheme(typeof(DataAggregatorActivityDesignerTheme))]
	public class DataAggregatorActivityDesigner : ExtendedActivityDesigner
	{
	}
	#endregion

	#region Class: DataAggregatorActivityDesignerTheme : Base: ActivityDesignerTheme
	public class DataAggregatorActivityDesignerTheme : ActivityDesignerTheme
	{
		public DataAggregatorActivityDesignerTheme(WorkflowTheme theme)
			: base(theme)
		{
			base.Initialize();
			this.BorderStyle = DashStyle.Solid;
			this.BorderColor = Color.YellowGreen;
			this.BackColorStart = Color.YellowGreen;
			this.BackColorEnd = Color.White;
			this.BackgroundStyle = LinearGradientMode.ForwardDiagonal;
			this.ForeColor = Color.Black;
		}
	}
	#endregion

	#region Class: DataAggregatorActivityValidator : Base: ActivityValidator
	public class DataAggregatorActivityValidator : ActivityValidator
	{
		public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
		{
			ValidationErrorCollection errors = new ValidationErrorCollection();
			DataAggregatorActivity activity = obj as DataAggregatorActivity;
			ActivityBind bind = activity.GetBinding(DataAggregatorActivity.ActivitiesClassProperty);
			if ( bind == null ) {
				errors.Add(new ValidationError("Data Aggregator Configuration not set.", 1914));
			}
			return errors;
		}
	}
	#endregion
}
